from PySide6.QtWidgets import QWidget,QAbstractItemView,QMenu,QTableWidgetItem,QMessageBox,QDialog
from PySide6.QtCore import Qt,QDate
import pandas as pd
import datetime
import uuid
from sqlalchemy.orm import Session
from sqlalchemy import extract

from settings.publicvar import engine

from views.initsetting.mode.initsetting import InitSetting
from views.issuedmange.printmaterial.printmateria import Ui_item
from views.issuedmange.model.issuedmange import IssuedPersonSure
from views.issuedmange.printmaterial.downdata.downdataView import DownoadDataView
from views.schoolInfo.model.SchoolInfo import SchoolInfo

class PrintmateriaView(QWidget,Ui_item):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.automenuData()
        self.surepersontypes = []
        self.serearchbtn.clicked.connect(self.serearchFun)
        self.savebtn.clicked.connect(self.saveFun)


    # 导航数据加载
    def automenuData(self):
        with Session(engine) as session, session.begin():
            self.issueddate.setDate(QDate.currentDate())
            initseting = session.query(InitSetting).one()
            name = initseting.school.name
            if name == '寻甸回族彝族自治县柯渡镇中心学校':
                schoolinfo = session.query(SchoolInfo).all()
                names = [item.name for item in schoolinfo]
                self.school.addItems(names)
            else:
                self.school.addItem(name)
            self.school.setCurrentText(name)
            self.issueddate.dateChanged.connect(self.issuedDateFun)

    # 表格加载数据
    def setTableStructure(self, data):
        self.itemtable.setRowCount(len(data))
        for i, item in enumerate(data):
            idx = QTableWidgetItem(str(item.id))
            self.itemtable.setItem(i, 0, idx)
            school = QTableWidgetItem(item.school)
            self.itemtable.setItem(i, 1, school)
            name = QTableWidgetItem(item.name)
            self.itemtable.setItem(i, 2, name)
            IDcard = QTableWidgetItem(item.IDcard)
            self.itemtable.setItem(i,3,IDcard)
            bankName= QTableWidgetItem(item.bankName)
            self.itemtable.setItem(i,4,bankName)
            cardNumber= QTableWidgetItem(item.cardNumber)
            self.itemtable.setItem(i,5,cardNumber)
            openbank= QTableWidgetItem(item.openbank)
            self.itemtable.setItem(i,6,openbank)
            amount = QTableWidgetItem(str(item.amount))
            self.itemtable.setItem(i,7,amount)
            paymenttype = QTableWidgetItem(item.paymenttype)
            self.itemtable.setItem(i,8,paymenttype)
            persontype = QTableWidgetItem(item.persontype)
            self.itemtable.setItem(i,9,persontype)
            issueddate= QTableWidgetItem(item.issueddate.strftime("%Y年%m月%d日"))
            self.itemtable.setItem(i,10,issueddate)
            purpose = QTableWidgetItem(item.purpose)
            self.itemtable.setItem(i, 11, purpose)
            uniqueID = QTableWidgetItem(item.uniqueID)
            self.itemtable.setItem(i,12,uniqueID)
            self.itemtable.setItem(i, 3, IDcard)
            bankName = QTableWidgetItem(item.bankName)
            self.itemtable.setItem(i, 4, bankName)
            cardNumber = QTableWidgetItem(item.cardNumber)
            self.itemtable.setItem(i, 5, cardNumber)
            openbank = QTableWidgetItem(item.openbank)
            self.itemtable.setItem(i, 6, openbank)
            amount = QTableWidgetItem(str(item.amount))
            self.itemtable.setItem(i, 7, amount)
            paymenttype = QTableWidgetItem(item.paymenttype)
            self.itemtable.setItem(i, 8, paymenttype)
            persontype = QTableWidgetItem(item.persontype)
            self.itemtable.setItem(i, 9, persontype)
            issueddate = QTableWidgetItem(item.issueddate.strftime("%Y年%m月%d日"))
            self.itemtable.setItem(i, 10, issueddate)
            purpose = QTableWidgetItem(item.purpose)
            self.itemtable.setItem(i,11,purpose)
            uniqueID = QTableWidgetItem(item.uniqueID)
            self.itemtable.setItem(i, 12, uniqueID)
            mark = QTableWidgetItem(item.mark)
            self.itemtable.setItem(i, 13, mark)


    # 搜索框数据处理
    def serearchFun(self):
        issueddate = self.issueddate.date()
        purpose = self.purpose.currentText().strip()
        if not purpose:
            QMessageBox.critical(None,'失败','用途不能空！')
        else:
            with Session(engine) as session,session.begin():
                issuedperson = session.query(IssuedPersonSure).filter(
                    extract('month', IssuedPersonSure.issueddate) == issueddate.month(),
                    extract('year', IssuedPersonSure.issueddate) == issueddate.year(),
                IssuedPersonSure.purpose == purpose).all()
                self.setTableStructure(issuedperson)
                self.showdata.setText('总共%s人下载数据包含数据上交，EXCEL数据导出，封面下载'%len(issuedperson))
                self.surepersontypes = [{'name':item.name,'IDcard':item.IDcard,'cardNumber':item.cardNumber,
                                         'amount':item.amount,'purpose':item.purpose,'paymenttype':item.paymenttype} for item in issuedperson]

    # 保存数据
    def saveFun(self):
        downloaddata = DownoadDataView(self.surepersontypes,(self.purpose.currentText(),self.school.currentText()))
        downloaddata.exec()

    # 日期处理函数
    def issuedDateFun(self):
        self.purpose.clear()
        issueddate = self.issueddate.date()
        with Session(engine) as session,session.begin():
            issuedperson = session.query(IssuedPersonSure).filter(extract('month',IssuedPersonSure.issueddate)==issueddate.month(),
                                                                  extract('year',IssuedPersonSure.issueddate)==issueddate.year()).all()
            purposes = set([item.purpose for item in issuedperson])
            self.purpose.addItems(purposes)